The User Presence Controller (attached to Gameboard) exposes all the presence features as well as keep an internal collection of the active users on the board.

The Gameboard node is always available in your game as long as the SDK has been integrated. In order to access it we just need to find it.

    var gameboard = GetNode("/root/GameboardSDK") as GameboardPlugin;

With access to our Gameboard node we can then move on to obtain a reference to the UserPresence controller. This is the controller that handles all the functions related to User Presence.

We will first need to define a reference to the controller to make it available throughout our script:

    UserPresenceController userPresenceController;

With our listener defined, we can now proceed to listen for user presence event. In our case we'll have a function called OnUserPresence handle this events.

We also want to make sure to initialize any users that were connected to the gameboard before the game started up, we can do this by checking if the userPresenceController is initialized. If it is initialized we will call OnUserPresenceControllerInitialized right away, and if it isn't we want to subscribe to the UserPresenceControllerInitialized event to fire off the OnUserPresenceControllerInitialized method whenever the userPresenceController is ready.

    userPresenceController = gameboard.GetNode("UserPresenceController") as UserPresenceController;

    userPresenceController.OnUserPresence += OnUserPresence;

    if (userPresenceController.IsInitialized)
    {
        OnUserPresenceControllerInitialized();
    }
    else
    {
        userPresenceController.UserPresenceControllerInitialized += OnUserPresenceControllerInitialized;
    }

Now that we are able to receive the user presence events all that remains is to handle them in the game.

Here we process any user presence updates in OnUserPresences, where any users that were around on initialization will be handled in OnUserPresenceControllerInitialized.

    void OnUserPresence(UserPresenceEventArgs userPresence)
    {
        // Handle changes in User Presence here.
    }

    void OnUserPresenceControllerInitialized()
    {
        foreach (var user in userPresenceController.Users.Values)
        {
            // Initialize existing users here
        }
    }

UserPresenceEventArgs useful properties

Although we are working in a managed environment it is always a good idea to clean the listeners when no longer needed.

    public override void _ExitTree()
    {
        userPresenceController.OnUserPresence -= OnUserPresence;
        userPresenceController.UserPresenceControllerInitialized -= OnUserPresenceControllerInitialized;
    }

This section include the entire code in one single, easy to copy section.

    UserPresenceController userPresenceController;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        var gameboard = GetNode("/root/GameboardSDK") as GameboardPlugin;
        userPresenceController = gameboard.GetNode("UserPresenceController") as UserPresenceController;

        userPresenceController.OnUserPresence += OnUserPresence;

        if (userPresenceController.IsInitialized)
        {
            OnUserPresenceControllerInitialized();
        }
        else
        {
            userPresenceController.UserPresenceControllerInitialized += OnUserPresenceControllerInitialized;
        }
    }

    void OnUserPresence(UserPresenceEventArgs userPresence)
    {
        // Handle User Presence here.
    }

    void OnUserPresenceControllerInitialized()
    {
        foreach (var user in userPresenceController.Users.Values)
        {
            // Initialize existing users here
        }
    }

    public override void _ExitTree()
    {
        userPresenceController.OnUserPresence -= OnUserPresence;
        userPresenceController.UserPresenceControllerInitialized -= OnUserPresenceControllerInitialized;
    }